#coding=utf-8
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from blog.forms import EMPTY_TITLE_ERROR, EMPTY_BODY_ERROR
from selenium import webdriver
import sys
import time

class FunctionalTest(StaticLiveServerTestCase):
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.maximize_window()
        self.browser.implicitly_wait(30)

    def tearDown(self):
        self.browser.refresh()
        self.browser.quit()
    
    def login(self):
        username = 'guohuixin'
        password = 'qianru831104'
        self.browser.get('http://localhost:8888/blog/')
        self.browser.find_element_by_css_selector('#login').click()
        self.browser.find_element_by_id('username').send_keys(username)
        self.browser.find_element_by_id('password').send_keys(password)
        self.browser.find_element_by_id('login-btn').click()
        return self.browser
        
    def test_home_title(self):
        self.browser.get('http://localhost:8888/blog/')
        time.sleep(2)
        self.assertIn("Home", self.browser.title)
    
    def test_login_url(self):
        self.browser.get('http://localhost:8888/blog/')
        time.sleep(2)
        self.browser.refresh()
        self.browser.find_element_by_css_selector("#login").click()
        blog_title = self.browser.find_element_by_id('blog_title').text
        self.assertEqual(u'萌宠乐园', blog_title)
        
    def test_login_username_pwd(self):
        self.browser = self.login()
        self.browser.refresh()
        username = self.browser.find_element_by_css_selector('#login h4 a').text
        self.assertIn('guohuixin', username)
    
    def test_logout(self):
        self.browser = self.login()
        self.browser.find_element_by_id('logout').click()
        login_text = self.browser.find_element_by_id('login').text
        self.assertEqual(login_text, u'登录')
    
    
    def test_login_add_blog_btn(self):
        self.browser = self.login()
        add_blog = self.browser.find_element_by_id('add-blog').text
        self.assertEqual(add_blog, u'+文章')
    
    def test_login_click_add_blog_cur_url(self):
        self.browser = self.login()
        self.browser.find_element_by_css_selector('.blog-btn a').click()
        self.browser.refresh()
        time.sleep(2)
        cur_win = self.browser.current_url
        print 'cur_win:', cur_win
        self.assertEqual(cur_win, 'http://localhost:8888/blog/add/')
        
    def test_validation_for_submit_blank_title(self):
        self.browser = self.login()
        self.browser.find_element_by_css_selector('.blog-btn a').click()
        self.browser.find_element_by_id('id_title').send_keys('')
        self.browser.find_element_by_id('id_body').send_keys('abc')
        self.browser.find_element_by_css_selector('.blog-btn-submit button').click()
        err_msg = self.browser.find_element_by_css_selector('.errorlist').text
        self.assertEqual(EMPTY_TITLE_ERROR, err_msg)
        
    def test_validation_for_submit_blank_body(self):
        self.browser = self.login()
        self.browser.find_element_by_css_selector('.blog-btn a').click()
        self.browser.find_element_by_id('id_title').send_keys('abc')
        self.browser.find_element_by_id('id_body').send_keys('')
        self.browser.find_element_by_css_selector('.blog-btn-submit button').click()
        err_msg = self.browser.find_element_by_css_selector('.errorlist').text
        self.assertEqual(EMPTY_BODY_ERROR, err_msg)
     
    def test_validation_for_submit_same_title(self):
        self.browser = self.login()
        self.browser.find_element_by_css_selector('.blog-btn a').click()
        self.browser.find_element_by_id('id_title').send_keys('abc')
        self.browser.find_element_by_id('id_body').send_keys('abc')
        self.browser.find_element_by_css_selector('.blog-btn-submit button').click()
        err_msg = self.browser.find_element_by_css_selector('.errorlist').text
        self.assertEqual(u'具有 标题 的 博客 已存在。', err_msg)
     
    def test_validation_for_comment(self):
        self.browser.get('http://localhost:8888/blog/article/1/')
        self.browser.find_element_by_class_name('comment-form').send_keys('content sth')
        self.browser.find_element_by_id('comment-btn').click()
        text_ = self.browser.find_element_by_css_selector('comment-body').text
        self.assertEqual(text_, 'content sth')
    